const response = require("../../utils/response.js");
const validate = require("../../utils/validate.js");
const productService = require("../services/productService.js");
const { URLSearchParams } = require('url');
const { createProductRules, deleteProductRules, updateProductRules, getProductRules } = require("../validations/productValidation.js");

class ProductController {
    async addProduct(ctx) {
        try {
            console.log(ctx.request.body)
            const productData = await validate(ctx, createProductRules);

            const  { id } = await productService.addProduct(productData);
            response.success(ctx, id, '添加成功');
        }catch(err) {
            console.log(err);
            response.error(ctx, err);
        }
    }
    async deleteProduct(ctx){
        try{
            const productData = await validate(ctx, deleteProductRules);

            const affectedRows = await productService.deleteProduct(productData.id);
            if(affectedRows > 0){
                response.success(ctx, productData.id, '删除成功')
            }else{
                response.error(ctx, '删除失败')
            }
        }catch(err){
            console.log(err)
            response.error(ctx, err);
        }
    }
    async updateProduct(ctx){
        try{
            const productData = await validate(ctx, updateProductRules);
            const affectedRows = await productService.updateProduct(productData);
            if(affectedRows > 0){
                response.success(ctx, '修改成功')
            }else{
                response.error(ctx, '修改失败')
            }
        }catch(err){
            console.log(err)
            response.error(ctx, err);
        }
    }
    async productInfo(ctx){
        if(ctx.state.product){
            return response.success(ctx, ctx.state.product)
        }else{
            return response.error(ctx, "登录状态已过期", null, 401)
        }
    }
    async getProductInfo(ctx){
        console.log('info')
        try{
            const productData = await validate(ctx, getProductRules);
            const row = await productService.getProductById(productData.id);
            if(row){
                response.success(ctx, row)
            }else{
                response.error(ctx, '获取失败')
            }
        }catch(err){
            console.log(err)
            response.error(ctx, err);
        }
    }
    async getProductListByPag(ctx){
        const usp = new URLSearchParams(ctx.querystring);
        let page = Number(usp.get('page')) || 1;
        let limit = Number(usp.get('limit')) || 10;
        let keyword = usp.get('keyword') || '';
        try{
            const { rows, count } = await productService.getProductListByPage(page, limit, keyword);
            response.success(ctx, {
                page,
                limit,
                rows,
                total: count
            });
        }catch(err){
            console.log(err)
            response.error(ctx, err);
        }
    }
}


module.exports = new ProductController();